#include <memory>
#include "Buzzer.h"

#include "common.h"


Buzzer::Buzzer() : BasicDevice(GENERAL_CONTROL_FILE) {	//for buzzer, extend card and so on
    //ctor
}

Buzzer::~Buzzer() {
    //dtor
}


void Buzzer::synchronizedBeep(BuzzerDataPtr pArray) {
    BuzzerDataPtr::element_type& data = *pArray;
    if (data.empty()){
        return;
    }

     BuzzerDataPtr::element_type::iterator begin = data.begin();
     ::ioctl(deviceFd, IOREQ_BUZZER_ON);
     usleep(*begin);
     ++begin;

    BuzzerDataPtr::element_type::iterator end = data.end();
    for(; begin != end; begin+= 2) {
        ::ioctl(deviceFd, IOREQ_BUZZER_OFF);
        usleep(*begin);

        ::ioctl(deviceFd, IOREQ_BUZZER_ON);
        usleep(*(begin + 1));
    }
    ::ioctl(deviceFd, IOREQ_BUZZER_OFF);
}



void Buzzer::beep(BuzzerDataPtr pArray) {
//     std::function<void(int, void*, void*)> r(std::bind(&Buzzer::synchronizedBeep, this, std::move(pArray), size, std::placeholders::_4));
//    r(0, nullptr);

    TaskRecordPtr pTaskRecord(new TaskRecord(TaskletInfo{std::bind(&Buzzer::synchronizedBeep, this, std::move(pArray)), nullptr}, std::chrono::system_clock::now().time_since_epoch()));
    longtimeTaskScheduler.addTask(pTaskRecord);
}

